package shellstation;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import java.util.ArrayList;
import java.sql.ResultSet;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

public class UpdateProductFormView extends JFrame implements ActionListener {

    private int z = 1;
    private int m = 0;
    private JPanel panel;
    private JButton submit;
    
    private ArrayList listSize = new ArrayList();

    private JLabel productName;
    private JLabel productPrice;
    private JLabel productStock;
    private JLabel status;
    
    private JLabel productStockTotal;
    
    private String proName;
    private String proPrice;
    private String proStock;

    private int proStock2;
    
    private JTextField pName;
    private JTextField pSize;
    private JTextField pPrice;
    private JTextField pStock;

    private int productID;
     
    private ResultSet rsData;

    private JComboBox box;
    private static String[] filename = {"Active", "Inactive"};

    private JComboBox box2;
   
    public void UpdateProductFormView(int rsID){

        
        ProductModel pr = new ProductModel();
        listSize = pr.getlistSize();
        
        ProductController pc = new ProductController();
        rsData = pc.getProductData(rsID);

        status = new JLabel("Status:");
        status.setBounds(80, 40, 200, 25 );

        productName = new JLabel("Product Name:");
        productName.setBounds(80, 90, 200, 25 );

        productPrice = new JLabel("Price:");
        productPrice.setBounds(80, 140, 200, 25 );

        productStock = new JLabel("Stock:");
        productStock.setBounds(80, 190, 200, 25);

        submit=new JButton("Update");
        submit.setBounds(130, 340, 200, 25 );
        submit.addActionListener(this);

        pName = new JTextField("",10);
        pName.setBounds(200, 90, 180, 25 );
        
        box2 = new JComboBox(filename);
        box2.setBounds(200, 40, 180, 25);

        pPrice = new JTextField("",10);
        pPrice.setBounds(200, 140, 180, 25 );

        pStock = new JTextField("",10);
        pStock.setBounds(200, 190, 180, 25 );

        productID = rsID;

        try {
                if (this.rsData.next()) {
                    try {  
                          proName = this.rsData.getString("productName");
                          proPrice = this.rsData.getString("price");
                          proStock2 = this.rsData.getInt("stock");
                          proStock = this.rsData.getString("stock");
                          
                    } catch (SQLException ex) {
                        JOptionPane.showMessageDialog(null, "System Error");
                    }
                   pName.setText(proName);
                   pPrice.setText(proPrice);
                   pStock.setText("0");

                }

            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, "System Error");
            }
        
         productStockTotal = new JLabel("Total Stock = " + proStock2);
        productStockTotal.setBounds(200, 210, 200, 25);
    }

    public void actionPerformed(ActionEvent e){

        if(e.getSource()== submit){
         String n = pName.getText();
         String p = pPrice.getText();
         String t = pStock.getText();
         String sb = box2.getSelectedItem().toString();
         
         if(n.isEmpty() || p.isEmpty() || t.isEmpty())
             JOptionPane.showMessageDialog(null, "Missing Values");
         else if(t.isEmpty() == false || p.isEmpty() == false){
             try {
         int x = Integer.parseInt(t);
            if(x<0) {
                m=1;
            JOptionPane.showMessageDialog(null,"Stock input must be a positive number");
            }
            else
         proStock2 = proStock2 + x;
            }
        catch(NumberFormatException nFE) {
        JOptionPane.showMessageDialog(null,"Stock input must be a number");
         m=1;}
           try {
                
                    Float f = Float.valueOf(pPrice.getText());
                    if(f <= 0){
                        m=1;
                        JOptionPane.showMessageDialog(null,"Price input must be a positive number");
                    }
                    
                } catch (NumberFormatException nFE) {
                  JOptionPane.showMessageDialog(null,"Price input must be a number");
                m=1;}
         if((!n.isEmpty() || !p.isEmpty() || !t.isEmpty()) && m ==0)
         this.z = JOptionPane.showConfirmDialog(null, "Are you sure you want to update this product?", "Confirm addition of product", JOptionPane.YES_NO_OPTION);
         if(z == 0){
            JOptionPane.showMessageDialog(null, "Product has been updateed!");
            ProductController pc = new ProductController();
             pc.getUpdateProduct(n, sb, p, proStock2, productID);
             try{
                 int x = Integer.parseInt(t);
                 pc.getQuantityUpdate(productID, x);
             }
             catch(NumberFormatException nFE){
             JOptionPane.showMessageDialog(null,"Stock Details table has been updated!");
             }
   
             UpdateProductView up = new UpdateProductView();
                up.UpdateProductView();
                panel.removeAll();
                panel.repaint();
                panel.add(up.getSearchButton());
                panel.add(up.getscrollPane2());
                panel.add(up.getsubmit());
                panel.add(up.getFilter());
                panel.add(up.getBox());
                panel.add(up.getSearch());
                panel.add(up.getSearchField());
                panel.add(up.getViewAllButton());
                up.setPanel(panel);

                panel.repaint();
                panel.revalidate();
         }
        
         }
        m=0; 

        }
    }
    public JLabel getStatus(){
        return status;
    }

    public JComboBox getbox(){
        return box;
    }

    public JComboBox getbox2(){
        return box2;
    }

    public JLabel getProductName(){
        return productName;
    }

    public JLabel getProductPrice(){
        return productPrice;
    }

    public JLabel getProductStock(){
        return productStock;
    }

     public JLabel getProductStockTotal(){
        return productStockTotal;
    }

    public JTextField getpName(){
        return pName;
    }

    public JTextField getpSize(){
        return pSize;
    }

    public JTextField getpPrice(){
        return pPrice;
    }

    public JTextField getpStock(){
        return pStock;
    }

    public JButton getsubmit(){
        return submit;
    }
    public void setPanel(JPanel panel2){
        
        panel = panel2;
    }
    
   
}